﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Application.MainModule.BankAccountsManagement;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;
using System.Web.Routing;

namespace Presentation.Web.MVC.Client.Controllers
{
    public class BankAccountController : Controller
    {
        #region private member

        /// <summary>
        /// Instance of the bank account service.
        /// </summary>
        private IBankAccountManagementService _bankAccountService;

        #endregion

        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="BankAccountController"/> class.
        /// </summary>
        /// <param name="bankAccountService">The bank account service.</param>
        public BankAccountController(IBankAccountManagementService bankAccountService)
        {
            _bankAccountService = bankAccountService;
        }

        #endregion

        #region public methods

        /// <summary>
        /// Display a paged listing of the existing bank accounts.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int? page, int? pageSize)
        {
            IList<BankAccount> bankAccounts = _bankAccountService.FindPagedBankAccounts(page.Value, pageSize.Value);
            return View(new BankAccountListViewModel(bankAccounts, page.Value, pageSize.Value));
        }

        /// <summary>
        /// Shows a specific bank account in detail.
        /// </summary>
        /// <param name="bankAccountId">The bank account id.</param>
        /// <returns>A view of the bank account details.</returns>
        public ActionResult Details(Guid bankAccountUserId)
        {
            //Get the bank account aggregate.
            List<BankAccount> bankAccounts = _bankAccountService.FindBankAccountsByUserId(bankAccountUserId);
            //Display its details.
            return View(bankAccounts.FirstOrDefault());
        }

        /// <summary>
        /// Displays a form to create a new bank account.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified bank account based on the request data and displays it.
        /// </summary>
        /// <param name="customer">The bank account to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(BankAccount bankAccount)
        {
            //Check if there aren't validation errors on the customer.
            if (ModelState.IsValid)
            {
                //Add bank account to the data base.
                _bankAccountService.AddBankAccount(bankAccount);
                //Show the first page of the bank account.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }

        /// <summary>
        /// Displays a form to edit the bank account.
        /// </summary>
        /// <param name="bankAccountId">The bank account code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid bankAccountUserId)
        {
            //Get the bank account aggregate to edit.
            List<BankAccount> bankAccount = _bankAccountService.FindBankAccountsByUserId(bankAccountUserId);
            //Return the
            return View(bankAccount);
        }

        /// <summary>
        /// Saves the changes on the editions performed to the specified bank account.
        /// </summary>
        /// <param name="bankAccount">The bank account.</param>
        /// <returns>The details of the bank account.</returns>
        [HttpPost]
        public ActionResult Edit(BankAccount bankAccount)
        {
            if (ModelState.IsValid)
            {
                _bankAccountService.ChangeBankAccount(bankAccount);
                return RedirectToAction("Details", new RouteValueDictionary() { { "bankAccountId", bankAccount.BankAccountId } });
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Deletes the specified bank account.
        /// </summary>
        /// <param name="bankAccountId">The bank account code.</param>
        /// <returns>The details of the bank account.</returns>
        public ActionResult Delete(Guid bankAccountUserId)
        {
            List<BankAccount> bankAccounts = _bankAccountService.FindBankAccountsByUserId(bankAccountUserId);
            foreach (BankAccount bankAccount in bankAccounts)
            {
                _bankAccountService.RemoveBankAccount(bankAccount);
            }
            
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }

        #endregion
    }
}
